package com.cty.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @Auther: cty
 * @Date: 2020/1/27 17:01
 * @Description:  整合配置文件
 * @version: 1.0
 */
public class Test07Server {
    private ServerSocket serverSocket ;
    public static void main(String[] args) {
        Test07Server server = new Test07Server();
        server.start();
    }
    //启动服务
    public void start() {
        try {
            serverSocket =  new ServerSocket(8888);
            receive();
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("服务器启动失败....");
        }
    }
    //接受连接处理
    public void receive() {
        try {
            Socket client = serverSocket.accept();
            System.out.println("一个客户端建立了连接....");
            //获取请求协议
            Request request =new Request(client);
            //获取响应协议
            Response response =new Response(client);
            //【通过请求中的URL，根据xml文件索引相应的类，最后通过反射获取相应的服务对象——xml文件配置url与服务对象的关系】
            Servlet servlet= WebApp.getServletFromUrl(request.getUrl());
            if(null!=servlet) {
                servlet.service(request, response);
                //关注了状态码
                response.pushToBrowser(200);
            }else {
                //错误....
                response.pushToBrowser(404);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.println("客户端错误");
        }
    }
    //停止服务
    public void stop() {

    }
}
